﻿ko.Application.prototype.groups = ko.observableArray();
ko.Application.prototype.files = ko.observableArray();
ko.Application.prototype.cdns = ko.observableArray();

ko.Application.prototype.selectedgroup = ko.observable();
ko.Application.prototype.selectedfile = ko.observable();
ko.Application.prototype.selectedcdn = ko.observable();

ko.Application.prototype.addGroup = function ( data, event ) {
    var self = ko.controller;

    self.add( 'group' );
};
ko.Application.prototype.addFile = function ( data, event ) {
    var self = ko.controller;

    if ( self.selectedgroup().groupId ) {
        self.add( 'file', { groupId: self.selectedgroup().groupId } );
    }
};
ko.Application.prototype.addCdn = function ( data, event ) {
    var self = ko.controller;

    if ( self.selectedfile().fileId ) {
        self.add( 'cdn', { fileId: self.selectedfile().fileId } );
    }
};
ko.Application.prototype.onGroupClick = function ( data, event ) {
    var self = ko.controller;
    self.files.splice( 0, self.files().length );
    if ( data.groupId ) {
        self.load( 'file', { groupId: data.groupId }, function () {
            self.onFileClick( self.files()[0], event );
        } );
    };

    self.selectedgroup( data );
};
ko.Application.prototype.onFileClick = function ( data, event ) {
    var self = ko.controller;
    self.cdns.splice( 0, self.cdns().length );
    if ( data.fileId ) {
        self.load( 'cdn', { fileId: data.fileId }, function () {
            self.onCdnClick( self.cdns()[0], event );
        } );
    };

    self.selectedfile( data );
}
ko.Application.prototype.onCdnClick = function ( data, event ) {
    var self = ko.controller;

    self.selectedcdn( data );
}

ko.Application.prototype.init = function () {
    var self = this;
    self.load( 'group', function () {
        self.onGroupClick( self.groups()[0] );
    } );
};

ko.groupModel = function ( group ) {
    var self = this;
    self.__typeof = 'group';

    self.groupId = group.groupId;
    self.groupName = ko.observable( group.groupName || '' );
    self.site = ko.observable( group.site || '' );

    self._editing = ko.observable( group._editing || false );
    self.event = {};
    self.event.edit = function ( group ) {
        group._editing( true );
    };
};

ko.fileModel = function ( file ) {
    var self = this;
    self.__typeof = 'file';

    self.groupId = file.groupId;
    self.fileId = file.fileId;
    self.filePath = ko.observable( file.filePath || '' );
    self.version = ko.observable( file.version || '' );

    self._editing = ko.observable( file._editing || false );
    self.event = {};
    self.event.edit = function ( file ) {
        file._editing( true );
    };
};

ko.cdnModel = function ( cdn ) {
    var self = this;
    self.__typeof = 'cdn';

    self.fileId = cdn.fileId;
    self.cdnId = cdn.cdnId;
    self.sitePath = ko.observable( cdn.sitePath || '' );
    self.useable = ko.observable( cdn.useable || true );

    self._editing = ko.observable( cdn._editing || false );
    self.event = {};
    self.event.edit = function ( cdn ) {
        cdn._editing( true );
    };
};